// pages/category/category.js
const app = getApp()
const api = require("../../utils/net.js")
const { $Toast } = require('../../dist/base/index')
const session = require("../../utils/session.js")
const util = require("../../utils/util.js")
const wxUtils = require("../../utils/wxUtils.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLoading: true,

    id:0,
    goodsCategory: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(!util.isEmptyStr(options.id)){
      this.setData({
        id: options.id
      });
    }
    
    session.check((msg) => {
      if (msg) {
        $Toast({ content: msg, type: 'error' })
        return
      }
      this.getGoodsCategory();
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },
  /**
   * 改变分类
   */
  changeCategory:function(e){
    let id = e.currentTarget.dataset.id;
    let index = e.currentTarget.dataset.index;
    let size = e.currentTarget.dataset.size;
    var line = parseInt(size / 3);
    this.setData({
      id:id
    })

    console.log("line:", line)
    console.log("index:", index)
    var lineCount=0
    var offset=0;
    for(var i=0;i<index;i++){
      var length=this.data.goodsCategory.childCategories[0].childCategories[i].childCategories.length
     
      lineCount +=parseInt(length/3) +1
      console.log("lineCount" + index, lineCount)
      
    }
    offset += lineCount * 115
    offset += 30*index;

     // 控制滚动
    wx.pageScrollTo({
      scrollTop: offset,
      duration: 300
    })
  },
  toCategorySearchPage:function(e){
    let categoryid = e.currentTarget.dataset.id;
    let categoryname = e.currentTarget.dataset.name;
    // if (id) {
    //   util.toPage("../categorySearch/categorySearch", { "id": id, "name": name  });
    // }else{
    //   wx.showToast({
    //     title: "分类id为空",
    //     icon: 'none',
    //     duration: 2000
    //   })
    // }

   // let datas = e.currentTarget.dataset
    //let categoryid = datas.categoryid;
    //let categoryname = datas.categoryname;
    util.toPage('/pages/search/search', {

      categoryId: categoryid,
      categoryName: categoryname
    })
  },
  /**
   * 获取商品分类列表
   */
  getGoodsCategory: function () {
    let that = this;
    api.get("goodsCategory",
      {}, {
        onNext: (res) => {
          let id = 0;
          if(that.data.id == 0){
            id = res.childCategories[0].childCategories[0].id;
            that.setData({
              goodsCategory: res,
              id: id
            })
          } else {
            that.setData({
              goodsCategory: res
            })
          }
        },
        onError: (msg, code) => {
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000
          })
        }
      })
  },
})